SpeechRecognitionのMicrophoneに入力した音声をnumpy arrayとして取り扱いたい
#SpeechRecognition
code:python
with sr.Microphone(sample_rate=16_000) as source:
audio_data = recognizer.listen(source)
frame_bytes = audio_data.get_raw_data()
audio_array = np.frombuffer(frame_bytes, dtype=np.int16)
get_raw_dataメソッドはbytesを返す
https://github.com/Uberi/speech_recognition/blob/3.8.1/reference/library-reference.rst#audiodata_instanceget_raw_dataconvert_rate-unionint-none--none-convert_width-unionint-none--none---bytes
numpy.frombufferでbytesをnumpy arrayに変換
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
デフォルトはdtype=float
これだとsounddeviceで再生できない(変換の確認のために再生したい)
sd.play(np.frombuffer(frame_bytes), 16_000)
code:python
>> np.frombuffer(frame_bytes)5000:5010 # 再生しているけれど、音になっていないのでは?
array([1.32261133e-182, 2.92995952e-207, 1.68786266e-214, 7.00875365e-180,
8.05298872e-197, 1.32506692e-233, 1.63840639e-281, 1.53057853e-251,
1.39560040e-188, 1.50013812e-205])
また、音声認識もうまくいかない
code:python
>> text, tokens, *_ = speech2text(np.frombuffer(frame_bytes))0
>> text
'ししししししししししし'